<?php


namespace App\Http\Controllers\Api\Repositories;

use App\Wen\Utils\FieldUtils;
use App\Wen\Utils\UserUtils;
use App\Wen\Utils\Utils;
use App\Models\WxChat as Model;
use Illuminate\Support\Carbon;

class WxContRepositories
{
    /**
     * 添加
     * @param [$uid,$feedback_type,$feedback_content]
     */
    public static function add($uid, $oid, $chat_content, $chat_image,$chat_audio_url, $chat_audio_length, $refer_id, $adoption_apply_id = null, $contact_apply_id = null, $shop_good_id = null, $used_good_id = null, $bureau_id = null, $voter_id = null)
    {
        $chatModel = new Model();

        $chatModel->user_id = $uid;
        $chatModel->object_id = $oid;
        $chatModel->chat_content = _mini_emoji($chat_content);
        $chatModel->chat_image = $chat_image;
        $chatModel->chat_audio_url = $chat_audio_url;
        $chatModel->chat_audio_length = $chat_audio_length;
        $chatModel->is_read = 0;
        $chatModel->chat_state = 0;

        if(!_empty_($refer_id)){
            $chatModel->refer_id = $refer_id;
            $chatModel->expand_type = 1;
            $chatModel->expand_id = $refer_id;
        }else{
            $chatModel->refer_id = null;
        }

        if($adoption_apply_id){
            $chatModel->adoption_apply_id = $adoption_apply_id;
            $chatModel->expand_type = 3;
            $chatModel->expand_id = $adoption_apply_id;
        }

        if($contact_apply_id){
            $chatModel->contact_apply_id = $contact_apply_id;
            $chatModel->expand_type = 2;
            $chatModel->expand_id = $contact_apply_id;
        }

        if(!_empty_($shop_good_id)){
            $chatModel->expand_type = 4;
            $chatModel->expand_id = $shop_good_id;
        }

        if(!_empty_($used_good_id)){
            $chatModel->expand_type = 5;
            $chatModel->expand_id = $used_good_id;
        }

        if(!_empty_($bureau_id)){
            $chatModel->expand_type = 7;
            $chatModel->expand_id = $bureau_id;
        }

        if(!_empty_($voter_id)){
            $chatModel->expand_type = 8;
            $chatModel->expand_id = $voter_id;
        }



        $r = $chatModel->save();
        if($r){
            if($chatModel->chat_audio_url && $chatModel->chat_audio_length > 0){
                $chat_content_ = '语音消息';
            }else if($chatModel->chat_image){
                $chat_content_ = '图片消息';
            }else if($chatModel->adoption_apply_id){
                $chat_content_ = '领养申请';
            }else if($chatModel->contact_apply_id){
                $chat_content_ = '联系信息申请';
            }else{
                if($chatModel->expand_type > 0){
                    if($chatModel->expand_type == 1){
                        $chat_content_ = '转发笔记';
                        $chatModel->refer_id = $chatModel->expand_id;
                    }else if($chatModel->expand_type == 2){
                        $chat_content_ = '联系信息申请';
                        $chatModel->contact_apply_id = $chatModel->expand_id;
                    }else if($chatModel->expand_type == 3){
                        $chat_content_ = '领养申请';
                        $chatModel->adoption_apply_id = $chatModel->expand_id;
                    }else if($chatModel->expand_type == 4){
                        $chat_content_ = '商品卡片';
                        $chatModel->shop_good_id = $chatModel->expand_id;
                    }else if($chatModel->expand_type == 5){
                        $chat_content_ = '闲置商品';
                        $chatModel->used_good_id = $chatModel->expand_id;
                    }else if($chatModel->expand_type == 7){
                        $chat_content_ = '组局活动';
                        $chatModel->bureau_id = $chatModel->expand_id;
                    }else if($chatModel->expand_type == 8){
                        $chat_content_ = '选票活动';
                        $chatModel->voter_id = $chatModel->expand_id;
                    }else if($chatModel->expand_type == 101){
                        $chat_content_ = '笔记审核推送';
                    }else if($chatModel->expand_type == 102){
                        $chat_content_ = '评论审核推送';
                    }else if($chatModel->expand_type == 103){
                        $chat_content_ = '组局审核推送';
                    }else if($chatModel->expand_type == 104){
                        $chat_content_ = '选票审核推送';
                    }else if($chatModel->expand_type == 105){
                        $chat_content_ = '选手审核推送';
                    }else if($chatModel->expand_type == 106){
                        $chat_content_ = '闲置审核推送';
                    }else if($chatModel->expand_type == 107){
                        $chat_content_ = '圈子审核推送';
                    }else if($chatModel->expand_type == 108){
                        $chat_content_ = '学生认证审核推送';
                    }else if($chatModel->expand_type == 109){
                        $chat_content_ = '身份认证审核推送';
                    }else if($chatModel->expand_type == 110){
                        $chat_content_ = '俱乐部审核推送';
                    }else{
                        // 101: 笔记审核 102：评论审核 103：组局审核 104：选票审核 105：选手审核 106：闲置审核
                        _logger_(__file__, __line__, '数据库expand_type不合法');
//                        return $this->fail(200006, [], '数据库expand_type不合法');
                    }
                }else{
                    $chat_content_ = _mini_emoji($chatModel->chat_content, true);
                }
            }
            Utils::templateMessage($oid, '收到了私信', $chat_content_, Carbon::parse($chatModel->created_at)->format('Y年m月d日 H:i'), '', '点击可查看详情');
            Utils::app_push(UserUtils::get_cached_user($uid)['user_name'], $chat_content_, 999, ['target_type'=>18, 'target_id'=>$uid], 'single', [$oid], 0);
            UserUtils::add_user_experience($uid, 7);
        }else{
            return null;
        }
        return $chatModel;
    }

}
